<template>
  <div>
    <!-- 交付物上传 -->
    <commElupData ref="commElupData" @initTableData="initTableData"></commElupData>
    <!-- 修改交付物 -->
    <commElRevise ref="commElRevise" @initTableData="initTableData" :sessionData="sessionDataModal"></commElRevise>
    <!-- 重命名 -->
    <commElRename ref="commElRename" @initTableData="initTableData" :sessionData="sessionDataModal"></commElRename>
    <!-- 编辑访问控制 -->
    <commElControlAuthority ref="commElControlAuthority" @initTableData='initTableData'></commElControlAuthority>
    <!-- 交付物状态设定 -->
    <commElStatusSetting ref="commElStatusSetting" @initTableData='initTableData'
                         :session-data="sessionDataModal"></commElStatusSetting>
    <!-- 移动交付物 -->
    <commElMobile ref="commElMobile" @initTableData='initTableData' :sessionData="sessionDataModal"></commElMobile>
    <!-- 删除 -->
    <commElDelete ref="commElDelete" @initTableData='initTableData' @initSigoView='initSigoView'
                  :sessionData="sessionDataModal"></commElDelete>
    <!-- 修订 -->
    <commElRevision ref="commElRevision" @initTableData='initTableData'
                    :sessionData="sessionDataModal"></commElRevision>
    <!-- 检入 -->
    <commElCheckIn ref="checkIn" @initTableData='initTableData' :sessionData="sessionDataModal"></commElCheckIn>
  </div>
</template>
<script>
import {saveFile} from '@/utils/util.js'
import {mapGetters} from 'vuex'
import {
  downloadDoc,
  checkoutObj,
  undoCheckoutObj,
  pasteObj,
  getPrimaryFileType,
  moveObjForAllVersion
} from "@/api/fileSingle.js"
import copyACtion from '@/views/singleProject/fileSingle/el-common-js/copy'
import commElupData from "@/views/singleProject/fileSingle/el-common/comm-el-upData.vue";
import commElRename from "@/views/singleProject/fileSingle/el-common/comm-el-rename.vue";
import commElControlAuthority from "@/views/singleProject/fileSingle/el-common/comm-el-ControlAuthority.vue";
import commElStatusSetting from "@/views/singleProject/fileSingle/el-common/comm-el-statusSetting.vue"
import commElMobile from "@/views/singleProject/fileSingle/el-common/comm-el-mobile.vue"
import commElDelete from "@/views/singleProject/fileSingle/el-common/comm-el-delete.vue";
import commElRevision from "@/views/singleProject/fileSingle/el-common/comm-el-revision.vue";
import commElCheckIn from "@/views/singleProject/fileSingle/el-common/comm-el-checkIn.vue";
import commElRevise from "@/views/singleProject/fileSingle/el-common/comm-el-revise.vue";
export default {
  mixins: [copyACtion],
  data() {
    return {
      overArr: ['download', 'startDocument', 'copy', 'cut', 'checkOut','unCheckOut', 'paste', 'startSignWorkflow'],
      modalStart: false,
      sessionDataModal: [],//存的session
      fileType: '',//文档类型
      fileName: '',//文档名称
    }
  },
  components: {
    commElupData,
    commElRename,
    commElControlAuthority,
    commElStatusSetting,
    commElMobile,
    commElDelete,
    commElRevision,
    commElCheckIn,
    commElRevise
  },
  computed: {
    ...mapGetters({
      'fileSingleData': 'fileSingle'
    })
  },
  mounted() {
  },
  methods: {
    /**
     * key 上级需要开启那个弹窗的key props上级tree选中的tree，propsArr上级table选中的数据
     */
    openElModal(key, sessionData, propsArr, ischeckHref) {
      this.sessionDataModal = sessionData
      // console.log(key)
      //console.log('开启弹窗时,传进来的数据',sessionData);
      if (this.overArr.indexOf(key) > -1) {
        /*if (key === 'download') this.download()
        if (key === 'copy') this.copy(this.fileSingleData.tableArr)
        if (key === 'paste') this.pasteMethod()
        if (key === 'cut') this.cut(this.fileSingleData.tableArr)
        if (key === 'checkOut') this.checkOut()*/
        if (key === 'startSignWorkflow') this.startDocument(sessionData)
        if (key === 'download') this.download(sessionData)
        if (key === 'copy') this.copy(sessionData)
        if (key === 'paste') this.pasteMethod()
        if (key === 'cut') this.cut(sessionData)
        if (key === 'checkOut') this.checkOut(sessionData)
        if (key === 'unCheckOut') this.unCheckOut(sessionData)
      } else {
        this.$refs[key].openModal(true,sessionData)
      }
    },
    /**
     * 启动文档
     */
    startDocument(sessionData) {
     // console.log(sessionData);
       this.fileSingleData.tableArr = sessionData
      // this.setFileSingle(this.fileSingleData)
      let sessArr = {
        tableArr:sessionData
      }
      
      sessionStorage.setItem('fileSingleDataByStart',JSON.stringify(sessArr))
      this.$router.push('fileSingle/fileStart');
    },
    /**
     * 下载
     */
    download(sessionData) {
      getPrimaryFileType({oid: sessionData[0].oid}).then((res) => {
        if (res && res.status == 200) {
          if (res.data && res.data.success) {
            this.fileType = res.data.data[0].fileType
            this.fileName = res.data.data[0].name
          } else {
            this.$message.error(res.data.message)
          }
        } else {
          this.$message.error('操作失败！')
        }
      })
      sessionData.forEach(el => {
        downloadDoc({oid: el.oid}).then((res) => {
          if (res) {
            let docType = '', docName = ''
            if (el.fileType != null && el.fileType != '' && el.fileType != 'undefined') {
              docType = el.fileType
            } else {
              docType = this.fileType
            }
            if (el.name != null && el.name != '' && el.name != 'undefined') {
              docName = el.name
            } else {
              docName = this.fileName
            }
            const blob = new Blob([res.data], {type: 'application/octet-stream'})
            saveFile(blob, docName + '.' + docType)
            this.$message.success('下载成功');
          } else {
            this.$message.warn('下载异常')
          }
        })
      })
    },
    /**
     * 粘贴
     */
    pasteMethod() {
      let props = this.paste()
      console.log('粘贴的数据：', this.copyOids(props))
      if (sessionStorage.getItem('coprOrParseEvent') == 2) {//剪切--做移动操作
        if (props.length > 0) {
          if (this.fileSingleData.layerItem == true) {
            this.moveFile()
          } else {
            this.$message.error('非文件夹最底层不可粘贴文档！')
          }
        } else {
          this.$message.error('暂无剪切的文档，请先剪切文档！')
        }
      } else {
        if (props.length > 0) {
          if (this.fileSingleData.layerItem == true) {
            pasteObj({newFolderOid: this.fileSingleData.treeItem.oid, items: this.copyOids(props)}).then((res) => {
              if (res && res.data.success) {
                /*if (props[0].isCut) {//剪切
                  deleteObjForAllVersion({oids: this.cycleID(props)}).then((res) => {
                  })
                }*/
                this.$message.success('粘贴成功！')
                this.initTableData()
              } else {
                this.$message.error(res.data.message)
              }
            })
          } else {
            this.$message.error('非文件夹最底层不可粘贴文档！')
          }
        } else {
          this.$message.error('暂无粘贴的文档，请先复制文档！')
        }
      }
    },
    /**
     * 检出方法
     */
    checkOut(sessionData) {
      let _this = this
      sessionData.forEach(item => {
        checkoutObj({oids: [item.oid]}).then((res) => {
          if (res && res.data.success) {
            _this.download([item])
            _this.$message.success(res.data.message);
            _this.initTableData()
          } else {
            this.$message.error(res.data.message)
          }
        })
      })
    },
    /**
     * 撤销检出方法
     */
    unCheckOut(sessionData) {
      let _this = this
      sessionData.forEach(item => {
        undoCheckoutObj({oids: [item.oid]}).then((res) => {
          if (res && res.data.success) {
            _this.$message.success(res.data.message);
            _this.initTableData()
          } else {
            this.$message.error(res.data.message)
          }
        })
      })
    },
    initTableData(type) {
      this.$emit('initTableData', 'ref_table')
    },
    initSigoView() {
      this.$emit('initSigoView')
    },
    //剪切--移动
    moveFile() {
      let data = [], newData
      newData = JSON.parse(sessionStorage.getItem('copyFileSingleTableData'))
      newData.forEach(el => {
        data.push({objOid: el.oid, folderOid: this.fileSingleData.treeItem.oid})
      })
      this.confirmLoading = true
      moveObjForAllVersion({items: data}).then((res) => {
        this.confirmLoading = false
        if (res && res.data.success) {
          this.$emit('initTableData')
          this.selectTreeArr = []
          this.visible = false
          this.$message.success('粘贴成功！')
        } else {
          this.$message.error(res.data.message)
        }
      })
    },
  }
}
</script>
<style lang="less" scoped>
/deep/ .ant-modal-content {
  .ant-modal-header {
    padding: 13px 21px;
    font-size: 16px;
    color: #333333;
    line-height: 24px;
  }
  .ant-modal-footer {
    .ant-btn {
      width: 72px;
    }
  }
}
/deep/ .ant-upload.ant-upload-drag {
  margin-left: 43px;
  width: 462px;
  height: 246px;
  background: #FBFBFB;
  border: 1px solid #D9D9D9;
  border-radius: 4px;
}
/deep/ .ant-upload-list {
  margin-left: 43px;
  width: 462px;
}
.ant-upload-text {
  font-size: 14px !important;
  color: #666666 !important;
  line-height: 22px;
}
.ant-upload-hint {
  font-size: 12px !important;
  color: #999999 !important;
  line-height: 20px;
}
.width_410 {
  width: 410px;
}
/deep/ .ant-form-item {
  display: flex;
}
.addBtn,
.delBtn {
  width: 120px;
  height: 32px;
  background: #FFFFFF;
  border: 1px solid #CCCCCC;
  border-radius: 4px;
}
.addBtn {
  margin-right: 9px;
}
.width_268 {
  width: 268px;
}
.width_509 {
  width: 509px;
}
.width_707 {
  width: 707px;
}
</style>